﻿using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Web.Models;

namespace ShopBack.Web.Controllers
{
    [Authorize]
    public class RoleController : Controller
    {
        // GET: Role
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 角色信息
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public ActionResult GetRole(int limit, int page, string Name)
        {
            int total = 0;
            List<Role> users = RoleService.GetRoles(limit, page, Name, out total).ToList();
            return Json(new
            {
                code = 0,
                msg = "",
                data = users,
                count = total
            }, JsonRequestBehavior.AllowGet);

        }

        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(Role role)
        {
            int a = RoleService.GetAdd(role);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "添加成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "添加失败" });
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult BatchDelArticele(string Ids)
        {
            int a = RoleService.GetBatchDelByIds(Ids);
            if (a > 0)
            {
                return Json(new { Success1 = true, Message = "删除成功" });
            }
            else
            {
                return Json(new { Success1 = false, Message = "删除失败" });
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Del(int id)
        {
            int a = RoleService.GetDel(id);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "删除成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "删除失败" });
            }
        }
        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Update(int id)
        {
            Role role = RoleService.GetSelect(id);
            return View(role);
        }
        /// <summary>
        /// 修改角色数据
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public ActionResult Update(Role role)
        {
            int a = RoleService.GetUpdate(role);
            if (a > 0)
            {
                return Json(new { Success = true, Message = "修改成功" });
            }
            else
            {
                return Json(new { Success = false, Message = "修改失败" });
            }
        }
        /// <summary>
        /// 修改角色模块页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult RoleModel(int id)
        {
            List<int> moduleId = RoleModuleService.GetModuleId(id);
            StringBuilder stb = new StringBuilder();
            foreach (var item in moduleId)
            {
                stb.Append(item + ",");
            }
            ViewBag.RoleId = id;
            ViewBag.ModuleId = stb.ToString().TrimEnd(',');
            return View();
        }
        /// <summary>
        /// 获取修改角色模块数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ModuleData()
        {
            List<Module> list = ModuleService.GetModelsMsg();
            List<UITree> resultlist = new List<UITree>();
            foreach (var item in list.Where(p => p.ParentId == 0))
            {
                resultlist.Add(new UITree
                {
                    id = item.Id,
                    title = item.ModuleName,
                    children = list.Where(p => p.ParentId == item.Id).Select
                  (p => new UITree { id = p.Id, title = p.ModuleName }).ToList()
                });
            }
            return Json(resultlist, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除角色模块
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult SetModule(string ids, int id)
        {
            if (ids == string.Empty)
            {
                return Json(new { Success = false, Message = "操作失败" });
            }
            else
            {
                int a = RoleModuleService.GetDel(ids, id);
                if (a > 0)
                {
                    return Json(new { Success = true, Message = "操作成功" });
                }
                else
                {
                    return Json(new { Success = false, Message = "操作失败" });
                }
            }
        }
        /// <summary>
        /// 添加角色模块
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult SetModuleAdd(string ids, int id)
        {

            int b = RoleModuleService.GetAdd(ids, id);
            if (b > 0)
            {
                return Json(new { Success = true, Message = "操作成功" });


            }
            else
            {
                return Json(new { Success = false, Message = "操作失败" });
            }
        }
    }
}